package IO流.IO流操作.处理流.演示装饰流原理;

/**
 * 模拟咖啡
 * 1.抽象组件：需要装饰的抽象对象（借口或抽象父类）
 * 2.具体组件：需要装饰的对象
 * 3.抽象装饰类：包含了抽象组件的引用以及装饰着共有的方法
 * 4.具体装饰类：被装饰的对象
 */
public class DecorateTest2 {
    public static void main(String[] args) {
        Drink coffee = new Coffee();
        Drink a = new Milk(coffee);
        System.out.println(a.cost()+"-"+a.info());
        Drink b = new suger(a);
        System.out.println(b.cost()+"-"+b.info());
    }
}

//抽象组件
interface Drink {
    double cost();//费用
    String info();//说明
}

//具体组件
class Coffee implements Drink {
    private String name = "原味咖啡";

    @Override
    public double cost() {
        return 10;
    }

    @Override
    public String info() {
        return name;
    }
}

//抽象装饰类
abstract class Decorate implements Drink {
    //对抽象组件的引用
    private Drink drink;

    public Decorate(Drink drink) {
        this.drink = drink;
    }

    @Override
    public double cost() {
        return this.drink.cost();
    }

    @Override
    public String info() {
        return this.drink.info();
    }
}

//具体装饰类1
class Milk extends Decorate{
    public Milk(Drink drink) {
        super(drink);
    }

    @Override
    public double cost() {
        return super.cost()*2;
    }

    @Override
    public String info() {
        return super.info()+"加入了牛奶";
    }
}

//具体装饰类2
class suger extends Decorate{
    public suger(Drink drink) {
        super(drink);
    }

    @Override
    public double cost() {
        return super.cost()*3;
    }

    @Override
    public String info() {
        return super.info()+"（加糖）";
    }
}
